using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Terra.Data.Entities;

namespace Terra.Data.Entities.Identity
{
    /// <summary>
    /// 部门实体
    /// </summary>
    [Table("Depts")]
    [Comment("部门表")]
    [Index(nameof(Name), IsUnique = true, Name = "IX_Dept_Name")]
    [Index(nameof(ParentId), Name = "IX_Dept_ParentId")]
    [Index(nameof(Sort), Name = "IX_Dept_Sort")]
    public class Dept : AuditEntityBase<Guid>
    {
        /// <summary>
        /// 部门名称
        /// </summary>
        [Required]
        [StringLength(50)]
        [Comment("部门名称")]
        public string Name { get; set; }

        /// <summary>
        /// 父部门ID
        /// </summary>
        [Comment("父部门ID")]
        public Guid? ParentId { get; set; }

        /// <summary>
        /// 部门编码
        /// </summary>
        [StringLength(50)]
        [Comment("部门编码")]
        public string Code { get; set; }

        /// <summary>
        /// 部门全称
        /// </summary>
        [StringLength(100)]
        [Comment("部门全称")]
        public string FullName { get; set; }

        /// <summary>
        /// 排序号
        /// </summary>
        [DefaultValue(100)]
        [Comment("排序号")]
        public int Sort { get; set; } = 100;

        /// <summary>
        /// 部门状态：1-启用，0-禁用
        /// </summary>
        [DefaultValue(1)]
        [Comment("部门状态：1-启用，0-禁用")]
        public int Status { get; set; } = 1;

        /// <summary>
        /// 部门负责人ID
        /// </summary>
        [Comment("部门负责人ID")]
        public Guid? LeaderId { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [StringLength(500)]
        [Comment("备注")]
        public string Remark { get; set; }

        /// <summary>
        /// 部门层级路径，使用 / 分隔的ID路径，如：/root/parent/current/
        /// </summary>
        [StringLength(500)]
        [Comment("部门层级路径")]
        public string Path { get; set; }

        /// <summary>
        /// 部门层级
        /// </summary>
        [Comment("部门层级")]
        public int Level { get; set; } = 1;

        /// <summary>
        /// 导航属性：父部门
        /// </summary>
        [ForeignKey(nameof(ParentId))]
        public Dept Parent { get; set; }

        /// <summary>
        /// 导航属性：子部门
        /// </summary>
        public ICollection<Dept> Children { get; set; }

        /// <summary>
        /// 导航属性：部门的用户
        /// </summary>
        public ICollection<User> Users { get; set; }
    }
}